<?php
	require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
	require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/doctype.php';
	
	//Get photo id
	$id = 0;
	if (isset($_GET['id'])) $id = $_GET['id'];

	//Get photo
	$result = mysql_query("SELECT * FROM photos WHERE id=$id") or die(mysql_error());
	if (mysql_num_rows($result) >= 1) $photo = mysql_fetch_array($result);
	else {
		//If no result, redirect
		header("Location:http://" . $_SERVER['HTTP_HOST'] . '/deviantpic/index.php');
		die();
	}

	//Get tags
	$tags	= "";
	$result = mysql_query("SELECT user_id FROM tags WHERE photo_id='$id' AND is_tagged='1'");
	$row	= mysql_fetch_array($result);

	//While data exist
	while ($row) {
		//Get user
		$user_result	= mysql_query("SELECT username FROM users WHERE id='" . $row['user_id'] . "'");
		$user_row		= mysql_fetch_array($user_result);

		//Append
		$tags .= $user_row['username'];

		//Next
		$row = mysql_fetch_array($result);

		//If not end, add ,
		if ($row) $tags .= ", ";
	}

	//Get keywords
	$keywords	= "";
	$result		= mysql_query("SELECT keyword FROM keywords WHERE photo_id='$id'");
	$row		= mysql_fetch_array($result);

	//While data exist
	while ($row) {
		//Append
		$keywords .= $row['keyword'];

		//Next
		$row = mysql_fetch_array($result);

		//If not end, add ,
		if ($row) $keywords .= ", ";
	}

	//Get list of categories
	$categories = array();

	$result = mysql_query("SELECT * FROM categories");
	$row	= mysql_fetch_array($result);
	while ($row) {
		$categories[] = $row;
		$row = mysql_fetch_array($result);
	}

	//Get view privileges
	$view_privileges = array();
	$result = mysql_query("SELECT * FROM photo_view_privileges");
	$row	= mysql_fetch_array($result);
	while ($row) {
		$view_privileges[] = $row;
		$row = mysql_fetch_array($result);
	}

	//Get comment privileges
	$comment_privileges = array();
	$result = mysql_query("SELECT * FROM photo_comment_privileges");
	$row	= mysql_fetch_array($result);
	while ($row) {
		$comment_privileges[] = $row;
		$row = mysql_fetch_array($result);
	}
?>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>deviantPIC - Edit photo</title>
    </head>
    <body>
		<!-- Header -->
		<?php require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/header.php'; ?>

		<!-- Content -->
		<div id="body">
			<h2>Edit <?php echo $photo['title'] ?> Information</h2>

			<!--  -->
			<div id="edit-picture">
				<form action=<?php echo '"view.php?id=' . $photo['id'] . '"'; ?> method="post"> <p>
					<!-- Name input -->
					<label for="input_title">Name: </label>
					<input type="text"
						   id="input_title"
						   name="title"
						   <?php
								echo "value='" . $photo['title'] . "'";
						   ?>
					/> <br />

					<!-- Categories -->
					<label for="input_category">Categories: </label>
					<select name="category" id="input_category">
						<?php
							foreach ($categories as $category) {
								$selected = "";
								if ($category['id'] == $photo['category_id']) $selected = " selected='selected'";

								echo '<option value="' . $category['id'] . '"' . $selected . '>' . $category['name'] . '</option>';
							}
						?>
					</select> <br />

					<!-- Description input -->
					<label for="input_desc">Description: </label>
					<input type="text"
						   id="input_desc"
						   name="description"
						   <?php
								echo "value='" . $photo['description'] . "'";
						   ?>
					/> <br />

					<!-- Keywords input -->
					<label for="input_keyword">Keywords: </label>
					<input type="text"
						   id="input_keyword"
						   name="keywords"
						   <?php
								echo "value='" . $keywords . "'";
						   ?>
					/> <br />

					<!-- Tags input -->
					<label for="input_tag">Tags: </label>
					<input type="text"
						   id="input_tag"
						   name="tags"
						   <?php
								echo "value='" . $tags . "'";
						   ?>
					/> <br />

					<!-- Date taken -->
					<label for="input_date">Date taken: </label>
					<input type="text"
						   id="input_date"
						   name="date"
						   <?php
								echo "value='" . $photo['date_taken'] . "'";
						   ?>
					/> <br />

					<!-- View privileges -->
					<label for="input_view-privilege">Who can see the picture: </label>
					<select name="view-privilege" id="input_view-privilege">
						<?php
							foreach ($view_privileges as $view_privilege) {
								$selected = "";
								if ($view_privilege['id'] == $photo['view_privilege']) $selected = " selected='selected'";

								echo '<option value="' . $view_privilege['id'] . '"' . $selected . '>' . $view_privilege['name'] . '</option>';
							}
						?>
					</select> <br />

					<!-- Comment privileges -->
					<label for="input_comment-privilege">Allow comment: </label>
					<select name="comment-privilege" id="input_comment-privilege">
						<?php
							foreach ($comment_privileges as $comment_privilege) {
								$selected = "";
								if ($comment_privilege['id'] == $photo['comment_privilege']) $selected = " selected='selected'";

								echo '<option value="' . $comment_privilege['id'] . '"' . $selected . '>' . $comment_privilege['name'] . '</option>';
							}
						?>
					</select> <br />

					<!-- Button -->
					<input type="submit" name="edit" value="Update" />
				</p></form>
			</div>
		</div>

		<!-- Footer -->
		<?php require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/footer.php'; ?>
    </body>
</html>